package com.itboye.boyeshoping.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.Toast
import com.alibaba.baichuan.trade.biz.login.AlibcLogin
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback
import com.itboye.boyeshoping.MainActivity

import com.itboye.boyeshoping.R
import com.itboye.boyeshoping.base.BaseActivity
import com.itboye.boyeshoping.presenter.ShopPresenter
import com.itboye.boyeshoping.util.Malert
import com.itboye.boyeshoping.util.SPUtils
import com.itboye.boyeshoping.util.volley.ResultEntity
import java.util.*

/**
 * Created by Administrator on 2018/7/11 0011.
 */

class LoginActivity : BaseActivity(), View.OnClickListener, Observer {

    private lateinit var taobaoLogin:LinearLayout

    private lateinit var mShopPrensenter: ShopPresenter

    private var  nickname:String = ""
    private var  openSid:String = ""
    private var  openId:String = ""
    private var  avatarUrl:String = ""
    private var  topAccessToken:String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        taobaoLogin=findViewById(R.id.taobaoLogin)
        mShopPrensenter= ShopPresenter(this)
        taobaoLogin.setOnClickListener(this)
//        if (SPUtils.get(application,null,"login",false) as Boolean){
//            startActivity(Intent(this@LoginActivity,MainActivity::class.java))
//            finish()
//        }
    }
    override fun onClick(v: View?) {
        when (v!!.id){
            R.id.taobaoLogin ->{
            login()

            }
        }
    }

    /**
     * 登录
     */
    fun login() {
        val alibcLogin = AlibcLogin.getInstance()
        alibcLogin.showLogin(object : AlibcLoginCallback {
            override fun onSuccess(i: Int) {
                //解析 session
//                Toast.makeText(this@LoginActivity, alibcLogin.session.toString(),Toast.LENGTH_LONG).show()
                Log.d("==========",alibcLogin.session.toString())
                val split = alibcLogin.session.toString().split(",".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()

                for (j in split.indices) {
                    var toTypedArray = split[j].split("=".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()
                    Log.d("======d",toTypedArray[0])
                    when {
                        toTypedArray[0].trim() == "nick" -> nickname=toTypedArray[1].trim()
                        toTypedArray[0].trim() == "ava" -> avatarUrl=toTypedArray[1].trim()
                        toTypedArray[0].trim() == "openId" -> openId=toTypedArray[1].trim()
                        toTypedArray[0].trim()==("openSid") -> openSid=toTypedArray[1].trim()
                        toTypedArray[0].trim()==("topAccessToken") -> topAccessToken=toTypedArray[1].trim()
                    }
                }

                mShopPrensenter.login(nickname,openSid,openId,avatarUrl,topAccessToken)


            }

            override fun onFailure(code: Int, msg: String) {
                Toast.makeText(this@LoginActivity, msg,
                        Toast.LENGTH_LONG).show()
            }
        })
    }


    /**
     * 退出登录
     */
    fun logout(view: View) {

        val alibcLogin = AlibcLogin.getInstance()

        alibcLogin.logout(object : AlibcLoginCallback {
            override fun onSuccess(i: Int) {
                Toast.makeText(this@LoginActivity, alibcLogin.toString(),
                        Toast.LENGTH_LONG).show()
            }

            override fun onFailure(i: Int, s: String) {
                Toast.makeText(this@LoginActivity, "登录失败 ",
                        Toast.LENGTH_LONG).show()
            }
        })
    }

    override fun update(o: Observable?, arg: Any?) {

        var entity = arg as ResultEntity
        if (entity!=null){

            if (entity.eventType==ShopPresenter.By_UserTbAccount_registerTb_success){
              Malert.alert(entity.data as String)
                startActivity(Intent(this@LoginActivity,MainActivity::class.java))
                SPUtils.put(this@LoginActivity,null,"login",true)
                finish()
            }else if (entity.eventType==ShopPresenter.By_UserTbAccount_registerTb_fail){
                Malert.alert(entity.msg)
            }
        }

    }

}
